package com.example.quartz;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.admin.entity.SysJob;
import com.example.admin.service.ISysJobService;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;


/**
 * 初始还定时任务
 */
@Component
public class JobInIt {

    @Autowired
    private Scheduler scheduler;
    @Autowired
    private ISysJobService sysJobService;


    @PostConstruct
    public void init() {
        QueryWrapper<SysJob> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", ScheduleConstants.Status.NORMAL);
        List<SysJob> jobs = sysJobService.list(queryWrapper);
        if (jobs != null && jobs.size() > 0) {
            jobs.forEach(job -> {
                try {
                    ScheduleUtils.createScheduleJob(scheduler, job);
                } catch (SchedulerException e) {
                    e.printStackTrace();
                }
            });
        }
    }
}
